<?php
defined('BASEPATH') OR exit('No direct script access allowed');
	class Article extends CI_Controller {
		public $page_name="article";
		public $validation_config = array(
		    array(
		        'field' => 'title',
		        'label' => '标题',
		        'rules' => 'required'
		    ),
		    array(
		        'field' => 'catalog_id',
		        'label' => '分类',
		        'rules' => 'required'
		    ),
		    array(
		        'field' => 'img',
		        'label' => '图片',
		        'rules' => 'required'
		    ),
		    array(
		        'field' => 'desc',
		        'label' => '描述',
		        'rules' => 'required'
		    ),
		    array(
		        'field' => 'content',
		        'label' => '内容',
		        'rules' => 'required'
		    )
		);
		public function __construct(){
    		parent::__construct();
    		if(!$this->session->userdata('is_login')){	//获取session数据
            	redirect('/admin/auth/login');
        	}
        }	
		public function index($page=1){
			$this->load->model('Article_model');
			$total=$this->Article_model->countall();
			$per_page=5;
			$data = array(
				'articles'=>$this->Article_model->get_by_page($page,$per_page)
			);
			$this->load->library('pagination');
			$config['base_url'] = site_url('/admin/article/index/');
			$config['total_rows'] =$total ;
			$config['per_page'] = $per_page;
			$this->pagination->initialize($config);			
			$this->load->view('admin/article_index',$data);
		}
		public function add(){
			$this->load->library('form_validation');
			$this->form_validation->set_rules($this->validation_config);	
            if ($this->form_validation->run() == FALSE){
            	$article=new stdClass();
				$article->title="";
				$article->catalog_id=0;
				$article->img="";
				$article->desc="";
				$article->content="";
            	$this->load->model('Catalog_model');
            	$data = array(
					'catalogs'=>$this->Catalog_model->get_catalog(),
					'title'=>"添加",
					'article'=>$article,
				);
            	$this->load->view('admin/Article_form',$data);
        	}else{
                $data = $this->input->post();
	        	$this->load->model("Article_model");
	            $this->Article_model->add_one($data);
	            redirect('admin/article/index');
            }
		}
		public function edit($id){
			$this->load->library('form_validation');
			$this->form_validation->set_rules($this->validation_config);
			$this->load->model("Article_model");		
            if ($this->form_validation->run() == FALSE){
            	$this->load->model('Catalog_model');
            	$data = array(
					'catalogs'=>$this->Catalog_model->get_catalog(),
					'article'=>$this->Article_model->get_one($id),
					'title'=>"编辑",
				);
            	$this->load->view('admin/Article_form',$data);
        	}else{
                $data = $this->input->post();	        	
	            $this->Article_model->edit_one($id,$data);
	            redirect('admin/article/index');
            }
		}
		public function del($id){
			$this->load->model('Article_model');
			$this->Article_model->del_id($id);
			redirect('admin/article/index');
		}
		public function recommend($id,$recommend){
			$this->load->model('Article_model');
			$this->Article_model->set_recommend($id,$recommend);
			redirect('admin/article/index');
		}			
	}	
